package com.moleq.mgpos.activities;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;


import com.moleq.base.MoleQBaseActivity;
import com.moleq.dialog.MessageBox;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.adapter.AppChooseAdapter;
import com.moleq.itemsetting.adapter.AppChooseAdapterIty;
import com.moleq.mginventory.activities.InventoryActivity;
import com.moleq.mginventory.activities.MGInventoryActivity;
import com.moleq.mginventory.activities.RecivingInquiryActivity;
import com.moleq.models.Functions;
import com.moleq.models.GlobalInfo;
import com.moleq.models.Users;
import com.moleq.service.FunctionService;
import com.moleq.service.ServiceManager;
import com.moleq.service.UsersService;


public class InventoryAppChooseActivity extends MoleQBaseActivity {

	private Context sharedContext = null;
//	private DBHelper dbHelper = null;
	private GridView gv_app_choose = null;
	private Button btn_app_choose_exit = null;
	//private MessageDialog mMessageDialog = null;
	private List<Functions> DataSource = null;
	private FunctionService functionService = null;
	private UsersService usersService = null;
	private AppChooseAdapterIty appChooseAdapterIty = null;
	
	private static final int Receiving = 1;
	private static final int Receiving_Inquiry = 2;
	private static final int Inventory = 3;
	private static final int User_Setting = 4;
	
	private GlobalInfo mGlobalInfo;
	private int posSettingID;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// Hidden title
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_inventory_app_choose);
		
		
		
		
		functionService = ServiceManager.getInstance().mInventoryFunctionService;
		usersService = ServiceManager.getInstance().mUsersService;
		btn_app_choose_exit = (Button) findViewById(R.id.btn_app_choose_exit);
		btn_app_choose_exit.setOnClickListener(mOnClickListener);
		gv_app_choose = (GridView) findViewById(R.id.gv_app_choose);
		mGlobalInfo = GlobalInfo.getInstance();
//		if (mGlobalInfo.cashier.equals("8888")) {
			DataSource = functionService.getInventoryfunctionlist();
//		} else {
//			DataSource = functionService.getuserfunctionlist(String
//					.valueOf(usersService.FindUserId(mGlobalInfo.cashier)));
//		}
		Users user = usersService.GetUserDetail(mGlobalInfo.cashier);
		appChooseAdapterIty = new AppChooseAdapterIty(this, DataSource,user);
		appChooseAdapterIty.setOnClickMessageListener(mOnClickAppChooseItem);
		gv_app_choose.setAdapter(appChooseAdapterIty);

		Drawable drawable = null;
		if (user.getUs_color() == 1 || mGlobalInfo.cashier.equals("8888")) {
			drawable = this.getResources().getDrawable(R.drawable.button_1);
		} else if (user.getUs_color() == 2) {
			drawable = this.getResources().getDrawable(R.drawable.button_2);
		} else if (user.getUs_color() == 3) {
			drawable = this.getResources().getDrawable(R.drawable.button_3);
		} else if (user.getUs_color() == 4) {
			drawable = this.getResources().getDrawable(R.drawable.button_4);
		} else if (user.getUs_color() == 5) {
			drawable = this.getResources().getDrawable(R.drawable.button_5);
		} else {
			drawable = this.getResources().getDrawable(R.drawable.button_1);
		}
		btn_app_choose_exit.setBackgroundDrawable(drawable);
	}

	public AppChooseAdapterIty.OnClickAppChooseItem mOnClickAppChooseItem = new AppChooseAdapterIty.OnClickAppChooseItem() {

		@Override
		public void onClickItem(int id) {
			// TODO Auto-generated method stub
			JumpToSetting(id);
		}

	};
	public OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_app_choose_exit:
				finish();
//				ShowMessage(
//						getResources().getString(R.string.msg_exit_program),
//						MessageDialog.MESSAGE_BOX_YES_NO,
//						MessageDialog.ICON_INFO);
//				MessageBox.show(AppChooseActivity.this, getResources().getString(R.string.msg_exit_program).toString()
//						, onClickMessageListener,MessageDialog.MESSAGE_BOX_YES_NO);
//				MessageBox.show(InventoryAppChooseActivity.this,InventoryAppChooseActivity.this.getResources().getString(R.string.msg_exit_program).toString()
//						, mOnClickMessageListener, MessageDialog.MESSAGE_BOX_YES_NO);
				break;

			default:
				break;
			}
		}

	};

//	public void ShowMessage(String text, int type, int icon) {
//		// if(mMessageDialog.isShowing()) return;
//		if (mMessageDialog != null && mMessageDialog.isShowing()) {
//			mMessageDialog.dismiss();
//		}
//		mMessageDialog = new MessageDialog(AppChooseActivity.this);
//		mMessageDialog.setStyle(type);
//		mMessageDialog.setIcon(icon);
//		mMessageDialog.setMessage(text);
//		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
//		mMessageDialog.show();
//	}

//	public OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {
//
//		@Override
//		public void onClickYes() {
//			// TODO Auto-generated method stub
//			finish();
//		}
//
//		@Override
//		public void onClickNo() {
//			// TODO Auto-generated method stub
//			mMessageDialog.dismiss();
//		}
//
//		@Override
//		public void onClickOK() {
//			// TODO Auto-generated method stub
//
//		}
//
//	};

//	OnClickMessageListener mOnClickMessageListener = new OnClickMessageListener() {
//		
//		@Override
//		public void onClickYes() {
//			// TODO Auto-generated method stub
//			finish();
//		}
//		
//		@Override
//		public void onClickOK() {
//			// TODO Auto-generated method stub
//			
//		}
//		
//		@Override
//		public void onClickNo() {
//			// TODO Auto-generated method stub
//			
//		}
//	};
	
	public void JumpToSetting(int id) {
		switch (id) {
		case Receiving:
			startActivity(new Intent(InventoryAppChooseActivity.this,
					MGInventoryActivity.class));
			break;
		case Receiving_Inquiry:
			startActivity(new Intent(InventoryAppChooseActivity.this,
					RecivingInquiryActivity.class));
			break;
		case Inventory:
			startActivity(new Intent(InventoryAppChooseActivity.this,
					InventoryActivity.class));
			break;
//		case User_Setting:
//			startActivity(new Intent(InventoryAppChooseActivity.this,
//					UserSettingActivity.class));
//			break;
		
		default:
			break;
		
			
		}
	}
	
	private OnClickMessageListener onDialogClickListener = new OnClickMessageListener() {

		public void onClickYes() {
//			ServiceManager.getInstance().mPosSettingService.updatePosNetwork(posSettingID);
//			startActivity(new Intent(AppChooseActivity.this,
//					NetworkReportActivity.class));
		}

		public void onClickNo() {
			
		}

		public void onClickOK() {
		}
	};
	
	private OnClickMessageListener mNoPosSettingListener = new OnClickMessageListener() {

		public void onClickYes() {
		}

		public void onClickNo() {
		}

		public void onClickOK() {
			finish();
		}
	};
}
